package com.example.superoa.model.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.dao.TitemDao;
import com.example.superoa.model.dao.TtestDao;
import com.example.superoa.model.mappers.TtestMapper;
import com.example.superoa.model.pojos.Titem;
import com.example.superoa.model.pojos.Ttest;

@Service
@Transactional(rollbackFor=Exception.class) 
public class TtestService {
		@Autowired
		private TtestMapper testmapper;    
		@Autowired
		private TtestDao testdao;
		@Autowired
		private StaffDao staffdao;		
		@Autowired
		private TitemDao titemdao;
		
		Shijian sj=new Shijian();
		//查询所有考试试卷
		public JSONArray findAllstest() {
			return testmapper.findAllstest();
		}
		//根据名字查询
		public JSONArray findBytestname(String ttestname) {
			return testmapper.findBytestname("%"+ttestname+"%");
		}
		//通过试卷id查看考生
		public JSONArray selectTStaff(Integer tid) {
			return testmapper.selectTtestStaff(tid);
		}
		//查询题目
		public JSONArray findAllDan(List<Integer> list,String str) {
			return testmapper.findAllDan(list,str);
		}
		//根据id查询题目
		public JSONObject findByIdDan(int id) {
			return testmapper.findByIdDan(id);
		}
		//根据题目id查询答案表
		public JSONObject findByIdDai(int id) {
			return testmapper.findByIdDai(id);
		}
		//根据id查看试卷详情
		public JSONArray findByIdtest(int ttestid) {
			return testmapper.findByIdtest(ttestid);
		}
		//新增试卷
		public void insertcopany(Ttest test,int staff_id) {
			test.setStaff(staffdao.findById(staff_id).get());
			test.setTtestdate(new Date(sj.getNetworkTime()));
			testdao.save(test);
		}
		//查询最近一次添加的试卷id
		public Integer selectid() {
			return testmapper.selectid();
		}
		//添加多对多表关系
		public void addDuo(int testid,Integer[] titemid) {
			Ttest test=testdao.findById(testid).get();
			List<Ttest> list;
			for (Integer id : titemid) {
				Titem titem=titemdao.findById(id).get();
				list=titem.getTtests();
				if(list==null) {
					list=new ArrayList();
				}
				list.add(test);
				titemdao.save(titem);
			}
		}
		//查看试卷
		public JSONObject selecttest(int id){
			return testmapper.findstest(id);
		}
		public JSONArray selecttitem(int id){
			return testmapper.selecttitem(id);
		}
		//初始化
		public JSONArray selectbyid(int ttestid) {
			return testmapper.findByIdtest(ttestid);
		}
		//修改试卷
		public void xiugai(int ttestid,String ttestname, String ttesttheme, String ttestledge,
			Date ttestdate, int ttesttnums) {
			Ttest test = testdao.findById(ttestid).get();
			test.setTtestname(ttestname);
			test.setTtesttheme(ttesttheme);
			test.setTtestledge(ttestledge);
			test.setTtestdate(ttestdate);
			test.setTtesttnums(ttesttnums);
			testdao.save(test);
		}
		//查看考生
		public JSONArray insertstudent(Integer ttestid) {
			return testmapper.findByIdtest(ttestid);			
		}
		//查看试卷
		public JSONArray selecttest() {
			return testmapper.selecttest();
		}		
}